package com.wsz.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsz.pojo.Album;
import com.wsz.pojo.R;
import com.wsz.user.service.AlbumService;
import com.wsz.utils.SecurityContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/album")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    @PostMapping("/save")
    public R saveAlbum(@RequestBody Album album){
        albumService.save(album);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R getAlbumsById(@PathVariable String id){
        System.out.println("id");
        List<Album> albumList = albumService.list(new QueryWrapper<Album>().eq("uid", id));
        return R.ok().data("albums",albumList);
    }


    @GetMapping("/getlist")
    public R getAlbums(){
        String userId = SecurityContextUtils.getUserId();

        List<Album> albumList = albumService.list(new QueryWrapper<Album>().eq("uid", userId));

        return R.ok().data("albums",albumList);
    }

    @DeleteMapping("/delete")
    private R deleteAlbum(@RequestParam("id") String id){
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",SecurityContextUtils.getUserId());
        queryWrapper.eq("id",id);

        boolean remove = albumService.remove(queryWrapper);

        if(remove) return R.ok();
        return R.error().message("系统🍐过大");
    }
}
